package Include;

import Common.REG;

import static java.lang.Math.*;

// 用于构造汇编代码的不同形式的指令
// 通过传入不同的操作数类型（如寄存器、立即数、内存地址等）
// ASMConstructor 类生成不同格式的汇编指令

public class ASMConstructor {
    public static String construct_asm(String op, int src) {
        return "\t" + op + " " + "[ ebp" + (src > 0 ? " - " : " + ") + abs(src) + " ]";
    }

    public static String construct_asm(String op) {
        return op;
    }

    public static String construct_asm(String op, String src) {
        return "\t" + op + " " + src;
    }

    public static String construct_asm(String op, REG src) {
        return "\t" + op + " " + src.name;
    }

    public static String construct_asm(String op, int dst, REG src) {
        return "\t" + op + " " + "[ ebp" + (dst > 0 ? " - " : " + ") + abs(dst) + " ]" + ", " + src.name;
    }

    public static String construct_asm(String op, int dst, String src) {
        return "\t" + op + " " + "[ ebp" + (dst > 0 ? " - " : " + ") + abs(dst) + " ]" + ", " + src;
    }

    public static String construct_asm(String op, String dst, String src) {
//        if (dst == null) return construct_asm(op, src);
        return "\t" + op + " " + src + ", " + dst;
    }

    public static String construct_asm(String op, String dst, REG src) {
//        if (dst == null) return construct_asm(op, src);
        return "\t" + op + " " + src.name + ", " + dst;
    }

    public static String construct_asm(String op, REG dst, String src) {
//        if (dst == null) return construct_asm(op, src);
        return "\t" + op + " " + dst.name + ", " + src;
    }

    public static String construct_asm(String op, REG dst, int src) {
//        if (dst == null) return construct_asm(op, src);
        return "\t" + op + " " + dst.name + ", " + "[ ebp" + (src > 0 ? " - " : " + ") + abs(src) + " ]";
    }

    public static String construct_asm(String op, REG dst, REG src) {
//        if (dst == null) return construct_asm(op, src);
        return "\t" + op + " " + dst.name + ", " + src.name;
    }

    public static String construct_asm(String op, REG dst, Integer dst_offset, REG src) {
        return "\t" + op + " " + construct_asm_mem(dst, dst_offset, "dword") + ", " + src.name;
    }

    public static String construct_asm(String op, REG dst, Integer dst_offset, int src) {
        return "\t" + op + " " + construct_asm_mem(dst, dst_offset, "dword") +
                ", " + "[ ebp" + (src > 0 ? " - " : " + ") + abs(src) + " ]";
    }

    public static String construct_asm(String op, REG dst, Integer dst_offset, String src) {
        return "\t" + op + " " + construct_asm_mem(dst, dst_offset, "dword") + ", " + src;
    }

    public static String construct_asm_mem(REG reg, int offset, String data_size) {
        return data_size + "[ " + reg.toString() + (offset > 0 ? " - " : " + ") + abs(offset) + " ]";
    }

    public static String construct_asm_mem(REG reg, int offset) {
        return "[ " + reg.toString() + (offset > 0 ? " - " : " + ") + abs(offset) + " ]";
    }
}


